#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 5005;
const int MX = 5001;

int n, m;
int a[N][N], sum[N][N];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) {
    int x, y, v;
    cin >> x >> y >> v;
    a[x + 1][y + 1] += v;
  }
  rep(i, 1, MX) rep(j, 1, MX) {
    sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + a[i][j];
  }
  int ans = 0;
  rep(i, m, MX) rep(j, m, MX) {
    int x = sum[i][j] - sum[i - m][j] - sum[i][j - m] + sum[i - m][j - m];
    ans = max(ans, x);
  }
  cout << ans;
  return 0;
}